package cn.yunhe.service.impl;

import cn.yunhe.dao.IUserMapper;
import cn.yunhe.model.Power;
import cn.yunhe.model.User;
import cn.yunhe.service.IUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements IUserService {
    @Resource
    private IUserMapper userMapper;

    /*public Page getAllUser(Map<String, Object> cond) {
        Page page = new Page();
        //总记录数
        int totalNum = userMapper.getUserCount(cond);
        page.setTotalNum(userMapper.getUserCount(cond));
        //每页记录数
        int pageSize = Integer.parseInt(String.valueOf(cond.get("pageSize")));
        page.setPageSize(pageSize);
        //当前页码
        int pageNum = Integer.parseInt(String.valueOf(cond.get("pageNum")));
        page.setPageNum(Integer.parseInt(String.valueOf(cond.get("pageNum"))));
        //总页数
        int totalPage = totalNum/pageSize + (totalNum%pageSize == 0 ? 0 : 1);
        page.setTotalPage(totalPage);
        page.setResult(userMapper.getAllUser(cond));
        return page;
    }*/

    @Override
    public Page queryLikeUsers(Map<String, Object> cond) {
        Page page = new Page();
        // 根据条件查询符合的用户列表记录总数，赋值给page的totalNum变量
        page.setTotal(userMapper.getLikeUsersCount(cond));
        // 从请求参数中获取每页大小
        int pageSize = Integer.parseInt(String.valueOf(cond.get("pageSize")));
        page.setPageSize(pageSize);
        // 从请求参数中获取当前页码
        int curPageNum = Integer.parseInt(String.valueOf(cond.get("pageNum")));
        page.setPageNum(curPageNum);
        //动态计算总页数(总记录数 除以 每页大小，加上  总页数 求余 每页大小，如果余数不为0，则 加 1，否则 加 0 )
        page.setPageNum((int) (page.getTotal() / pageSize + (page.getTotal() % pageSize == 0 ? 0 : 1)));
        //根据条件查询符合的用户列表记录，赋值给page的result变量
        page = PageHelper.startPage(curPageNum, pageSize);
        List list = userMapper.getLikeUsers(cond);
        return page;
    }

    @Override
    public void addUser(User user) {


        userMapper.addUser(user);
    }

    @Override
    public void delUser(User user) {
        userMapper.delUser(user);
    }

    @Override
    public User findUser(User user) {
        User user1 = userMapper.findUser(user);
        return user1;
    }

    @Override
    public User login(User user) {
        User u = userMapper.login(user);
        return u;
    }

    @Override
    public void editUser(User user) {
        userMapper.editUser(user);
    }

    @Override
    public User queryUsername(User user) {
        return userMapper.queryUsername(user);
    }

    @Override
    public Power qetPowerByUrl(String url) {
        Power power = userMapper.qetPowerByUrl(url);
        System.out.println(power.getRole());
        return power;
    }

    public IUserMapper getUserMapper() {
        return userMapper;
    }

    public void setUserMapper(IUserMapper userMapper) {
        this.userMapper = userMapper;
    }


}
